import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
  history: createWebHashHistory(),
  routes: []
});

export default router;

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('./components/Login.vue') },
  {
    path: '/home',
    component: () => import('./components/Home.vue'),
    redirect: '/home/users',
    children: [
      { path: 'users', component: () => import('./components/subcomponents/MyUsers.vue') },
      { path: 'rights', component: () => import('./components/subcomponents/MyRights.vue') },
      { path: 'goods', component: () => import('./components/subcomponents/MyGoods.vue') },
      { path: 'orders', component: () => import('./components/subcomponents/MyOrders.vue') },
      { path: 'settings', component: () => import('./components/subcomponents/MySettings.vue') },
      { path: 'menu', component: () => import('./components/subcomponents/MyMenu.vue') }
    ]
  }
];



// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 如果目标路径是登录页面，直接放行
  if (to.path === '/login') {
    return next();
  }

  // 获取本地存储中的 token
  const token = localStorage.getItem('token');

  // 如果没有 token，重定向到登录页面
  if (!token) {
    return next('/login');
  }

  // 如果有 token，允许跳转
  next();
});